import 'package:demo/app/widgets/custom_indicator.dart';
import 'package:demo/theme/app_colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../model/order_model.dart';
import '../../../theme/app_text_theme.dart';
import 'order_controller.dart';

class OrderView extends GetView<OrderController> {
  const OrderView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var index = 0;
    switch(Get.arguments['type']){
      case 1: // 待确认
        index = 1;
        break;
      case 2: // 待还款
        index = 2;
        break;
      case 3: // 已逾期
        index = 4;
        break;
      case 4: // 审核中
        index = 1;
        break;
    }
    return Container(
      color: Color_bg,
      child: DefaultTabController(
        length: controller.myTabs.length,
        initialIndex: index,
        child: Scaffold(
          backgroundColor: Color_bg,
          appBar: AppBar(
            title: Text('我的订单',style: text_title_black),
            bottom: TabBar(
              tabs: controller.myTabs,
              indicatorWeight: 2.0, // 设置指示器的高度
              labelColor:Color_main,
              labelStyle: text_main_14,
              indicator:CustomIndicator(),
              unselectedLabelColor:Color_grey,
              unselectedLabelStyle:text_grey_14,
              indicatorPadding: EdgeInsets.symmetric(horizontal: 20.w),
              isScrollable : true,
              dividerHeight: 0,
              tabAlignment: TabAlignment.start,
              labelPadding : EdgeInsets.only(left:15.w,right: 15.w,top: 10.w,bottom: 10.w),
            ),
          ),
          body: TabBarView(
            children: <Widget>[
              page1(),
              page2(),
              page3(),
              page4(),
              page5(),
            ],
          ),
        ),
      ),
    );
  }

  Widget page1(){
    return Obx(() => SmartRefresher(
      enablePullDown: true,
      enablePullUp: controller.canLoadMore1.value,
      controller: controller.refreshController1,
      onRefresh: ()=>controller.getLoanList1(true),
      onLoading: ()=>controller.getLoanList1(false),
      child: controller.list1.isNotEmpty ? setItem(controller.list1):Center(child: Text('暂无订单',style: text_grey_18)),
    ));
  }
  Widget page2(){
    return Obx(() => SmartRefresher(
      enablePullDown: true,
      enablePullUp: controller.canLoadMore2.value,
      controller: controller.refreshController2,
      onRefresh: ()=>controller.getLoanList2(true),
      onLoading: ()=>controller.getLoanList2(false),
      child: controller.list2.isNotEmpty ? setItem(controller.list2):Center(child: Text('暂无订单',style: text_grey_18)),
    ));
  }
  Widget page3(){
    return Obx(() => SmartRefresher(
      enablePullDown: true,
      enablePullUp: controller.canLoadMore3.value,
      controller: controller.refreshController3,
      onRefresh: ()=>controller.getLoanList3(true),
      onLoading: ()=>controller.getLoanList3(false),
      child: controller.list3.isNotEmpty ? setItem(controller.list3):Center(child: Text('暂无订单',style: text_grey_18)),
    ));
  }
  Widget page4(){
    return Obx(() => SmartRefresher(
      enablePullDown: true,
      enablePullUp: controller.canLoadMore4.value,
      controller: controller.refreshController4,
      onRefresh: ()=>controller.getLoanList4(true),
      onLoading: ()=>controller.getLoanList4(false),
      child: controller.list4.isNotEmpty ? setItem(controller.list4):Center(child: Text('暂无订单',style: text_grey_18)),
    ));
  }
  Widget page5(){
    return Obx(() => SmartRefresher(
      enablePullDown: true,
      enablePullUp: controller.canLoadMore5.value,
      controller: controller.refreshController5,
      onRefresh: ()=>controller.getLoanList5(true),
      onLoading: ()=>controller.getLoanList5(false),
      child: controller.list5.isNotEmpty ? setItem(controller.list5):Center(child: Text('暂无订单',style: text_grey_18)),
    ));
  }
  setItem(List data) {
    return ListView.separated(
      shrinkWrap : true,
      physics:const BouncingScrollPhysics(),
      padding: EdgeInsets.zero,
      itemCount: data.length,
      separatorBuilder: (BuildContext context, int index) => const Divider(color:Colors.transparent),
      itemBuilder: (BuildContext context, int index) {
        var bean = data[index] as Order;
        var str = '';
        var color = const Color(0x00000000);
        switch(bean.status){
          case 1:
          case 2:
            str = '已结清';
            color = Color_grey;
            break;
          case 10:
            str = '已过期';
            color = Color_grey;
            break;
          case 11:
            str = '已逾期';
            color = Color_red;
            break;
          case 3:
            str = '授信中';
            color = Color_grey;
            break;
          case 6:
            str = '放款中';
            color = Color_grey;
            break;
          case 4:
            str = '授信通过';
            color = Color_main;
            break;
          case 7:
            str = '放款成功';
            color = Color_main;
            break;
          case 5:
            str = '授信拒绝';
            color = Color_red;
            break;
          case 8:
            str = '放款失败';
            color = Color_red;
            break;
          case 9:
            str = '待还款';
            color = Color_red;
            break;
        }
        return InkWell(
          highlightColor: Colors.transparent, // 透明色
          splashColor: Colors.transparent, // 透明色
          onTap: () => controller.toDetail(bean),
          child: Container(
            margin: EdgeInsets.only(left: 15.w,right: 15.w,top:index==0?15.w:0,bottom: index == data.length - 1 ? 15.w : 0),
            padding: EdgeInsets.symmetric(horizontal: 12.w,vertical: 12.w),
            decoration: BoxDecoration(
              color: Color_white,
              borderRadius: BorderRadius.all(Radius.circular(8.w)),
            ),
            child: Column(
              crossAxisAlignment : CrossAxisAlignment.start,
              children: [
                Text('订单编号：${bean.loanNum}',style: text_grey_12),
                SizedBox(height: 9.w),
                Container(height: 0.2,color: Color_line),
                SizedBox(height: 9.w),
                Row(
                  children: [
                    Expanded(
                      child: Column(
                          crossAxisAlignment : CrossAxisAlignment.start,
                          children: [
                            Text('授信额度',style: text_131732_14),
                            SizedBox(height: 7.w),
                            Text('${bean.loanAmount}',style: text_131732_20.copyWith(fontSize: 24.sp))
                          ]),
                    ),
                    RawMaterialButton(
                      highlightColor: Colors.transparent, // 透明色
                      splashColor: Colors.transparent, // 透明色
                      elevation: 0,
                      shape : RoundedRectangleBorder(side : BorderSide(color: color,width: 0.5),borderRadius : BorderRadius.all(Radius.circular(6.w))),
                      constraints: BoxConstraints.tightFor(width: 80.w, height: 30.w),
                      child: Text(str,style: text_grey_12.copyWith(color: color)),
                      onPressed: ()=> controller.toDetail(bean),
                    ),
                  ],
                ),
                SizedBox(height: 3.w),
              ],
            ),
          ),
        );
      },
    );
  }
}
